package com.zgw.his.design.model;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.internal.resources.Resource;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;

import com.zgw.his.design.common.LocalResourceHelp;

public class Folder extends MedicalResource {
	private List<Template> templates = new ArrayList<Template>();
	private List<Folder> chidrens = new ArrayList<Folder>();
	private Folder parent;
	private String name; // 名称
	private String number; // 编号,标识
	private String path;
	private File file;
	private int state = STATE_NOMAL;

	public Folder(Folder parent, String name) {
		this.parent = parent;
		this.name = name;
		if (parent != null) {
			parent.chidrens.add(this);
			this.path = parent.path + name;
		}
	}

	protected void addTemplate(Template template) {
		if (!templates.contains(template)) {
			templates.add(template);
		}
	}

	public List<Template> getTemplates() {
		return templates;
	}

	public List<Folder> getChidren() {
		return chidrens;
	}

	public Folder getParent() {
		return parent;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	private List<Folder> folderAll;

	/**
	 * 查找该Folder 和 其所有子Folder
	 * 
	 * @return
	 * */
	public final List<Folder> findFolderAll() {
		List<Folder> list = new ArrayList<Folder>();
		list.add(this);
		findFolders(this, list);
		return list;
	}

	/**
	 * 查找该Folder 和 其所有子Folder
	 * 
	 * @return
	 * */
	public final List<Template> findTemplateAll() {
		List<Template> list = new ArrayList<Template>();
		findTemplates(this, list);
		return list;
	}

	/**
	 * 装载 f0下所有 <b> 子目录和孙目录</b>
	 * */
	public static void findFolders(Folder f0, List<Folder> list) {
		list.addAll(f0.getChidren());
		for (Folder f : f0.getChidren()) {
			findFolders(f, list);
		}
	}

	/**
	 * 装载 f0下所有 <b>子模版和孙模版</b>
	 * */
	public static void findTemplates(Folder f0, List<Template> list) {
		list.addAll(f0.getTemplates());
		for (Folder f : f0.getChidren()) {
			findTemplates(f, list);
		}
	}

	public void remove(Folder chidren) {
		if (chidrens.contains(chidren)) {
			chidrens.remove(chidren);
		}
	}

	public void remove(Template template) {
		if (templates.contains(template)) {
			templates.remove(template);
		}
	}

	@Override
	public Resource getResource() {
		if (path != null)
			return LocalResourceHelp.getResource(path, IResource.FOLDER);
		return null;
	}

	/**
	 * 获得默认编号
	 * */
	public String getDefaultNumber() {
		return path.replace('/', '.').substring(1);
	}
}
